package com.kalua.list.dbaccess.user;

import org.springframework.cassandra.core.RowMapper;

import com.datastax.driver.core.Row;
import com.datastax.driver.core.exceptions.DriverException;
import com.kalua.list.dbaccess.DAOConstants;
import com.kalua.list.domainmodel.UUIDIdentifier;
import com.kalua.list.domainmodel.geo.CountryCode;
import com.kalua.list.domainmodel.security.EmailAddress;
import com.kalua.list.domainmodel.security.MobilePhoneNumber;
import com.kalua.list.domainmodel.security.User;
import com.kalua.list.domainmodel.security.UserImpl;

public class UserRowMapper implements RowMapper<User>, DAOConstants {
	
	public static final String USER_COL_FAMILY_NAME = "user";
	
	public static final String COUNTRY_CODE_COL_NAME = "country_code";
	public static final String PHONE_NUMBER_COL_NAME = "phone_number";
	public static final String VERIFIED_COL_NAME = "verified";
	public static final String PREF_NAME_COL_NAME = "preferred_name";
	public static final String EMAIL_COLUMN_NAME = "email";

	@Override
	public User mapRow(Row row, int rowNumber) throws DriverException {				
		return UserRowMapper.mapToEntity(row, rowNumber); 
	}

	public static UserImpl mapToEntity(Row row, int rowNumber) {
		UserImpl result;
		
		EmailAddress emailAddress = 
				new EmailAddress(row.getString(EMAIL_COLUMN_NAME));
		UUIDIdentifier id = new UUIDIdentifier(row.getUUID(ACCOUNT_REF_COLUMN_NAME));
		String preferredName = row.getString(PREF_NAME_COL_NAME);
		boolean verified = row.getBool(VERIFIED_COL_NAME);
		MobilePhoneNumber phoneNumber = 
					new MobilePhoneNumber(row.getString(PHONE_NUMBER_COL_NAME));
		CountryCode countryCode = 
					new CountryCode(row.getString(COUNTRY_CODE_COL_NAME));
		
		result = new UserImpl(	emailAddress, id, 
											preferredName, verified, 
											phoneNumber, countryCode);
		return result;
	}
}